maps-frontend.js ➔ ... ➔ ???   A
last analyzed

Complexity

Conditions 1
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 2
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
nop 0
1
import GoogleMap from './blocks/maps/modules/GoogleMap';
2
import {createRandomId} from './blocks/maps/functions/createRandomId';
3
4
class App {
5
    constructor() {
6
        window.GoogleMap = {
7
            readyApi: false,
8
            api: document.querySelector('meta[property="maps:key"]').getAttribute('content'),
9
            instances: [],
10
            init: () => {
11
                [].forEach.call(document.querySelectorAll('*[data-module="google-map"]'), container => {
12
                    let instanceId = createRandomId();
13
                    let options = container.getAttribute('data-options');
14
15
                    if (typeof options == 'undefined' || options == '') {
16
                        options = {};
17
                    } else {
18
                        options = JSON.parse(options);
19
                    }
20
21
                    window.GoogleMap.instances.push(new GoogleMap(container, Object.assign({
22
                        instanceId: instanceId
23
                    }, options)));
24
                });
25
            }
26
        };
27
    }
28
29
    init() {
30
        if (!window.GoogleMap.readyApi && (window.GoogleMap.api != '' || typeof window.GoogleMap.api != 'undefined')) {
31
            let script = document.createElement('script');
32
33
            script.type = 'text/javascript';
34
            script.src = `//maps.googleapis.com/maps/api/js?key=${window.GoogleMap.api}&libraries=places&callback=window.GoogleMap.init`;
35
36
            document.body.appendChild(script);
37
            window.GoogleMap.readyApi = true;
38
        }
39
    }
40
}
41
42
document.addEventListener("DOMContentLoaded", () => {
43
    if (!('GoogleMap' in window)) {
44
        const APP = new App();
45
46
        APP.init();
47
    }
48
});